package mymatrix;

import comm.ClientPriority;

import client.Client;

public class MatrixMultiplicationClient extends Client {
	private final int size = 1000;

	private long[][] matrixA = new long[size][size];
	private long[][] matrixB = new long[size][size];

	public MatrixMultiplicationClient(ClientPriority priority,
			int problemNumber, int version, String masterAddress,
			int masterPort, String codeServerAddress, int codeServerPort) {
		super(priority, problemNumber, version, masterAddress, masterPort,
				codeServerAddress, codeServerPort);
		// TODO Auto-generated constructor stub
	}

	private void populate() {
		for (int i = 0; i < this.size; i++) {
			for (int j = 0; j < this.size; j++) {
				this.matrixA[i][j] = 1;
				this.matrixB[i][j] = j;
			}
		}

	}

	public void execute() {
		populate();
//		printMatrix(matrixA);
//		printMatrix(matrixB);
		for (int i = 0; i < size; i++) {
			MatrixMultTask mt = new MatrixMultTask(i, i, size, matrixA[i],
					matrixB);
			commit(mt);
		}

		long result[][] = new long[size][];

		for (int i = 0; i < size; i++) {
			MultResut mr = ((MultResut) (getNextResult()));
			result[mr.getLine()] = mr.getResultVector();
		}
		printMatrix(result);
		System.exit(0);
	}

	private void printMatrix(long[][] result) {
		for (int i = 0; i < result.length; i++) {
			System.out.print("[");
			for (int j = 0; j < result[0].length; j++) {
				System.out.print(" " + result[i][j]);
			}
			System.out.println(" ]");
		}
		System.out.println("----");

	}

	public static void main(String[] args) {
		new MatrixMultiplicationClient(ClientPriority.NORMAL, 2, 1,
				"localhost", 0, "localhost", 0).execute();
	}

}
